package com.power.sifa.correction.dao.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.plf.core.common.dao.OffersetTransformers;
import com.power.sifa.correction.dao.Apl_condeploy_personDaoCustom;
import com.power.sifa.correction.model.Apl_condeploy_person;
import com.power.sifa.correction.model.pageInfo.Apl_condeploy_personPageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.query.internal.NativeQueryImpl;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* 社区矫正人员布控人员信息 持久层实现(hibernate)
*/

@Slf4j
@Repository
public class Apl_condeploy_personDaoImpl implements Apl_condeploy_personDaoCustom{

    private final EntityManager entityManager;

    public Apl_condeploy_personDaoImpl(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    public List<Apl_condeploy_person> findList(Apl_condeploy_personPageInfo pageInfo) {
        List<Apl_condeploy_person> result = new ArrayList<>();

        StringBuffer sql = new StringBuffer("select a.*, b.subtime, b.instid from apl_condeploy_person a left join apl_main b on  a.id = b.id where b.instid is not null");
        List<Object> paramsList = new ArrayList<>();
        int index = 1;

        // 社矫人员
        if (StringUtils.isNotEmpty(pageInfo.getF_name())) {
            sql.append(" and a.name like ?" + index++);
            paramsList.add("%" + pageInfo.getF_name() +"%");
        }
        // 状态
        if (ObjectUtil.isNotNull(pageInfo.getF_status())) {
            sql.append(" and b.status in ?" + index++);
            paramsList.add(pageInfo.getF_status());
        }
        // 录入人
        if (StringUtils.isNotEmpty(pageInfo.getF_reguserid())) {
            sql.append(" and b.reguserid = ?" + index++);
            paramsList.add(pageInfo.getF_reguserid() );
        }
        // 提交时间
        if (ObjectUtil.isNotEmpty(pageInfo.getF_subtime())) {
            sql.append(" and b.subtime >= ?" + index++);
            paramsList.add(pageInfo.getF_subtime());
        }
        if (ObjectUtil.isNotEmpty(pageInfo.getF_subtime_01())) {
            sql.append(" and b.subtime <= ?" + index++);
            paramsList.add(pageInfo.getF_subtime_01());
        }

        // 矫正开始时间
        if (ObjectUtil.isNotEmpty(pageInfo.getF_startdate())) {
            sql.append(" and a.startdate >= ?" + index++);
            paramsList.add(pageInfo.getF_startdate());
        }
        if (ObjectUtil.isNotEmpty(pageInfo.getF_startdate_01())) {
            sql.append(" and a.startdate <= ?" + index++);
            paramsList.add(pageInfo.getF_startdate_01());
        }

        // 布控开始时间
        if (ObjectUtil.isNotEmpty(pageInfo.getF_condate())) {
            sql.append(" and a.constartdate >= ?" + index++);
            paramsList.add(pageInfo.getF_condate());
        }
        if (ObjectUtil.isNotEmpty(pageInfo.getF_condate_01())) {
            sql.append(" and a.constartdate <= ?" + index++);
            paramsList.add(pageInfo.getF_condate_01());
        }

        // 布控编号
        if (ObjectUtil.isNotEmpty(pageInfo.getF_concode())) {
            sql.append(" and a.concode like ?" + index++);
            paramsList.add("%" + pageInfo.getF_concode() + "%");
        }
        // 罪名
        if (ObjectUtil.isNotEmpty(pageInfo.getF_crime())) {
            sql.append(" and a.crime like ?" + index++);
            paramsList.add("%" + pageInfo.getF_crime() + "%");
        }
        // 执行地
        if (ObjectUtil.isNotEmpty(pageInfo.getF_exeaddr())) {
            sql.append(" and a.exeaddr like ?" + index++);
            paramsList.add("%" + pageInfo.getF_exeaddr() + "%");
        }

        // 排序
        sql.append(" order by b.subtime desc");
        Query query = entityManager.createNativeQuery(sql.toString());
        query.unwrap(NativeQueryImpl.class).setResultTransformer(OffersetTransformers.ALIAS_TO_ENTITY_MAP);
        log.info(sql.toString());

        // 添加条件
        for (int i = 0; i <= paramsList.size() - 1; i++) {
            query.setParameter(i + 1, paramsList.get(i));
        }
        // 查询总数
        pageInfo.setAllcount(query.getResultList().size());
        // 分页
        if (pageInfo.getAllcount() > 0) {
            int pageStart = (pageInfo.getPageNumber() - 1) * pageInfo.getPageSize();
            List<Map> list = query.setFirstResult(pageStart).setMaxResults(pageInfo.getPageSize()).getResultList();
            if (CollectionUtil.isNotEmpty(list)) {
                result = JSON.parseArray(JSON.toJSONString(list), Apl_condeploy_person.class);
            }
        }
        return result;
    }
}
